﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using XFControls.Utils;
using XDAFacebook;

// ReSharper disable CheckNamespace

namespace XFControls.XFPanels.XFPanelItems
// ReSharper restore CheckNamespace
{
    public enum XFItemTabs
    {
        One,
        Two,
        Three,
        None
    }

    public class XFPanelListEventArgs : EventArgs
    {
        public XFPanelListEventArgs(XFPanelList list)
        {
            List = list;
        }

        public XFPanelList List { get; set; }
    }

    ///<summary />
    public class XFItemThreeTabs : XFItemBase
    {
        private readonly int _tabHeigt = 52;
        private readonly int _textPadding = 14;
        private XFItemTabs _curTab;
        private XFItemTabs _selTab;

        public XFItemThreeTabs(XFItemTabs selected)
            : this()
        {
            _curTab = selected;
        }

        public XFItemThreeTabs()
        {
            _curTab = XFItemTabs.One;
            _selTab = XFItemTabs.None;
            Style = new XFItemStyle
                    {
                        TextFont = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular),
                        Padding = 5
                    };

            ItemType = XFItemType.Clickable;

            if (XFGraphics.GetScreenResolutionNotRotated() == ScreenResolution.QVGA)
            {
                _textPadding /= 2;
                _tabHeigt /= 2;
            }
        }

        public string TabOne { get; set; }
        public string TabTwo { get; set; }
        public string TabThree { get; set; }

        public event EventHandler<XFPanelListEventArgs> OneClick;
        public event EventHandler<XFPanelListEventArgs> TwoClick;
        public event EventHandler<XFPanelListEventArgs> ThreeClick;

        public override int GetHeight()
        {
            return _tabHeigt;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            int width = GetWidth();
            int tabWidth = (width / 3);

            using (var b = new SolidBrush(Color.RoyalBlue))
            {
                g.FillRectangle(b, x, y, GetWidth(false), GetHeight());
            }
            if (!string.IsNullOrEmpty(Style.BackgroundImageString))
            {
                DrawUI(Style.BackgroundImageString, g, new Rectangle(0, 0, GetWidth(), GetHeight()));
            }

            var recOne = new Rectangle(x, y, tabWidth, _tabHeigt);
            var recTwo = new Rectangle(x + tabWidth, y, tabWidth, _tabHeigt);
            var recThree = new Rectangle(x + (tabWidth * 2), y, tabWidth, _tabHeigt);

            switch (_curTab)
            {
                case XFItemTabs.One:
                    drawSelectedTab(g, recOne, TabOne);
                    drawUnselectedTab(g, recTwo, TabTwo);
                    drawUnselectedTab(g, recThree, TabThree);
                    break;
                case XFItemTabs.Two:
                    drawUnselectedTab(g, recOne, TabOne);
                    drawSelectedTab(g, recTwo, TabTwo);
                    drawUnselectedTab(g, recThree, TabThree);
                    break;
                default:
                    drawUnselectedTab(g, recOne, TabOne);
                    drawUnselectedTab(g, recTwo, TabTwo);
                    drawSelectedTab(g, recThree, TabThree);
                    break;
            }
        }

        public override XFItemClickResult GetClickResult()
        {
            if (_selTab == XFItemTabs.None || _selTab == _curTab || Parent == null)
            {
                _selTab = XFItemTabs.None;
                return XFItemClickResult.Nothing;
            }

            _curTab = _selTab;
            _selTab = XFItemTabs.None;

            Invalidate(false, ThreadPriority.Normal);

            var par = Parent as XFPanelList;
            if (par != null)
            {
                int pos = par[this] + 1;
                par.ClearItems(pos);
            }

            switch (_curTab)
            {
                case XFItemTabs.One:
                    if (OneClick != null)
                        OneClick(this, new XFPanelListEventArgs(Parent as XFPanelList));
                    break;
                case XFItemTabs.Two:
                    if (TwoClick != null)
                        TwoClick(this, new XFPanelListEventArgs(Parent as XFPanelList));
                    break;
                case XFItemTabs.Three:
                    if (ThreeClick != null)
                        ThreeClick(this, new XFPanelListEventArgs(Parent as XFPanelList));
                    break;
            }

            ResetHeight();
            return XFItemClickResult.Nothing;
        }

        public override void OnMouseDown(int x, int y)
        {
            base.OnMouseDown(x, y);
            int width = Screen.PrimaryScreen.WorkingArea.Width;
            int tabWidth = (width / 3);
            if (x < tabWidth)
                _selTab = XFItemTabs.One;
            else if (x < (tabWidth * 2))
                _selTab = XFItemTabs.Two;
            else
                _selTab = XFItemTabs.Three;
        }

        private void drawSelectedTab(Graphics g, Rectangle rec, string text)
        {
            if (this.GetImageRec("selected-tab").Height == 0)
            {
                using (var b = new SolidBrush(Color.White))
                    g.FillRectangle(b, rec);
            }
            else
                DrawUI("selected-tab", g, rec, false);
            using (var b = new SolidBrush(Color.FromArgb(29,29,29)))
                g.DrawString(text, Style.TextFont, b, rec.X + _textPadding, rec.Y + _textPadding);
        }

        private void drawUnselectedTab(Graphics g, Rectangle rec, string text)
        {
            if (this.GetImageRec("unselected-tab").Height == 0)
            {
                rec.Height -= Style.PaddingBottom * 2;
                rec.Y += Style.PaddingBottom;

                using (var b = new SolidBrush(Color.SteelBlue))
                    g.FillRectangle(b, rec);
            }
            else
                DrawUI("unselected-tab", g, rec, false);
            using (var b = new SolidBrush(Color.FromArgb(29,29,29)))
                g.DrawString(text, Style.TextFont, b, rec.X + _textPadding, rec.Y + _textPadding);
        }

        public override void Dispose()
        {
            base.Dispose();

            OneClick = null;
            TwoClick = null;
            ThreeClick = null;
        }
    }
}